package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.AdminUserDao;
import ex.com.jm.food.entity.User;
/**
 * dao实现层
 * @author zhaojw
 *
 */
public class AdminUserDaoImpl extends HibernateDaoSupport implements
		AdminUserDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<User> getUsers(final int initPage,final int pageRow,final String name,
			final String s_date,final String e_date,final Integer type,final String sort,final String order) {
		// TODO Auto-generated method stub
		Object users=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from User user where 1=1 ";
				if(name!=null&&!name.equals("")){
					hql +=" and user.userName like '%"+name+"%' ";
				}
				if (s_date!=null&&!s_date.equals("")) {
					hql += " and user.created >= '"+s_date+"'";
				}
				if(e_date !=null && e_date.equals("")){
					hql += " and user.created <='"+e_date+"'";
				}
				if(type!=null && !type.equals("")){
					hql += " and user.type ="+type;
				}
				if(sort!=null&&!sort.equals("")&&order!=null&&!order.equals("")){
					hql += " order by user."+order+" "+sort;
				}else {
					hql +=" order by user.created desc";
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<User>) users;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int userCount(final String name,final String s_date,final String e_date,final Integer type) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="from User user where 1=1 ";
				if(name!=null&&!name.equals("")){
					hql +=" and user.userName like '%"+name+"%' ";
				}
				if (s_date!=null&&!s_date.equals("")) {
					hql += " and user.created >= '"+s_date+"'";
				}
				if(e_date !=null && e_date.equals("")){
					hql += " and user.created <='"+e_date+"'";
				}
				if(type!=null && !type.equals("")){
					hql += " and user.type ="+type;
				}
				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@Override
	public User getUser(String id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(User.class, id);
	}

	@Override
	public Object saveUser(User user) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(user);
		
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public boolean updateType(final Integer type,final String id) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="update User user set user.type="+type+" where user.userId='"+id+"'";
				Query query=session.createQuery(hql);
				int count=query.executeUpdate();
				return count;
			}
		});
		if(count==1){
			return true;
		}else{
		return false;
		}
	}

	@Override
	public void delUser(String id) {
		// TODO Auto-generated method stub
		User user=this.getHibernateTemplate().get(User.class, id);
		this.getHibernateTemplate().delete(user);
	}

	@Override
	public void updateUser(User user) {
		// TODO Auto-generated method stub
		this.getHibernateTemplate().update(user);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public User checkName(final String name) {
		User user=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("from User where userName='"+name+"'");
				return query.uniqueResult();
			}
		});
		return user;
		
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public User checkEmail(final String email) {
		
		User user=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("from User where userEmail='"+email+"'");
				return query.uniqueResult();
			}
		});
		return user;
		
	}

	

}
